package cn.cdut.servlet;

import cn.cdut.servlet.tools.Email.E_mail;
import cn.cdut.servlet.tools.JDBC.Destroyed;
import cn.cdut.servlet.tools.JDBC.Sign;
import cn.cdut.servlet.tools.JDBC.tools;

import javax.jws.WebService;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.sql.rowset.JdbcRowSet;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;

@WebServlet(urlPatterns = "/demo1")
public class Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        boolean temp = false;
        HttpSession session = request.getSession();
        request.setCharacterEncoding("UTF-8");
        String email = request.getParameter("email");
        email = new String(email.getBytes(StandardCharsets.ISO_8859_1), "UTF-8");
        String psd = request.getParameter("psd");
        psd = new String(psd.getBytes(StandardCharsets.ISO_8859_1), "UTF-8");
        if ("go".equals(request.getParameter("submit"))) {
            try {
                temp = tools.find(email, psd);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            if (temp == true) {
                psd = new String(psd.getBytes(StandardCharsets.ISO_8859_1), "UTF-8");
                Sign.dflag = 0;
                session.setAttribute("email", email);
                String s = "/Welcome.jsp";
                response.sendRedirect(s);

            } else {
                Sign.dflag = 1;
                response.sendRedirect("login.jsp");
            }
        } else if ("up".equals(request.getParameter("submit"))) {
            String repsd = request.getParameter("repsd");
            repsd = new String(psd.getBytes(StandardCharsets.ISO_8859_1), "UTF-8");
            session.setAttribute("email", email);
            session.setAttribute("repsd", repsd);
            session.setAttribute("psd", psd);
            if (Sign.code == null) {
                Sign.zflag = 2;
                response.sendRedirect("register.jsp");
            } else if (false == request.getParameter("psd").equals(request.getParameter("repsd"))) {
                Sign.zflag = 3;
                response.sendRedirect("register.jsp");
            } else if (( request.getParameter("code") == null) || (Sign.zflag == 0 && request.getParameter("code") == "")) {
                Sign.zflag = 4;
                response.sendRedirect("register.jsp");
            } else if (false==(Sign.code.equals(request.getParameter("code").toString()))) {
                System.out.println("cuo");
                Sign.zflag = 4;
                response.sendRedirect("register.jsp");
            } else {
                try {
                    temp = tools.find(email);
                    if (temp == true) {
                        Sign.zflag = 1;
                        response.sendRedirect("register.jsp");

                    } else {
                        tools.insert(email, psd);
                        Sign.dflag = 2;
                        response.sendRedirect("login.jsp");
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
